package com.ytjj.qmyx.supplychain.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * 获取token
 *
 *
 * @version 1.0
 * 
 */
public class TokenUtil {

    public static String getToken() {
        // 从 http 请求头中取出 token
        String token = getRequest().getHeader("token");
        return token;
    }

    public static String getUuid() {
        // 从 http 请求头中取出 uuid
        String uuid = getRequest().getHeader("uuid");
        return uuid;
    }

    public static String getSourcePlatform() {
        // 从 http 请求头中取出 sourcePlatform
        String sourcePlatform = getRequest().getHeader("sourcePlatform");
        return Optional.ofNullable(sourcePlatform).orElse("x");
    }

    public static String getPartentId() {
        String partentId = getRequest().getHeader("partentId");
        return partentId;
    }

    /**
     * 获取request
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getRequest();
    }

    public static String getDataType(){
        String dataType = getRequest().getHeader("platformType");
        return Optional.ofNullable(dataType).orElse("sale");
    }

    public static String getChannel(){
        // 判断平台
        String source = getSource();
        String channel = getRequest().getHeader("channel");
        if("ios".equals(source)){
            channel = "ios";
        }
        if("weapp".equals(source) && StringUtils.isEmpty(channel)){
            channel = "weapp";
        }
        return Optional.ofNullable(channel).orElse("");
    }

    public static String getSource(){
        String source = getRequest().getHeader("source");
        return source;
    }

    public static String getPlatformData(){
        String source = getRequest().getHeader("source");
        return source == null || "h5".equals(source) || "quick-app".equals(source) ? "h5":"app";
    }

    public static String getImei(){
        String imei = getRequest().getHeader("imei");
        return imei;
    }
    public static String getOaid(){
        String oaid = getRequest().getHeader("oaid");
        return oaid;
    }

    public static Integer getOsVersion(){
        String osVersion = getRequest().getHeader("osVersion");
        Integer version = null;
        try{
            version = StringUtils.isEmpty(osVersion) ? null: Integer.parseInt(osVersion);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return version;
    }

    public static String getCha() {
        return getRequest().getHeader("channel");
    }

    public static String getPlatform(){
        return getRequest().getHeader("source");
    }
}

